﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WPFTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region FIELDS

        private bool IsCapturing = false;
        private Windows.CaptureWindow  cFrame;
        private OzProject Project; 

        #endregion

        #region CONSTRUCTORS

        public MainWindow()
        {
            InitializeComponent();
            InitializeEventHooks();
            
            Project = new OzProject("Untitled");
            listBox1.DataContext = Project;
            OzTextFrame testFrame = new OzTextFrame();

        }

        #endregion

        #region METHODS

        public void SaveProjectToFile()
        {
            System.Windows.Forms.FolderBrowserDialog folderDlg = new System.Windows.Forms.FolderBrowserDialog();
            folderDlg.Description = "Select the output folder.";
            folderDlg.ShowNewFolderButton = true;

            System.Windows.Forms.DialogResult dlgResult = folderDlg.ShowDialog();

            if (dlgResult == System.Windows.Forms.DialogResult.OK)
            {
                string folder = folderDlg.SelectedPath + "/";
                MessageBox.Show("Output folder is: " + folderDlg.SelectedPath, "Debug message( this will be removed later )", MessageBoxButton.OKCancel);

                Project.ProjectName = ProjectNameTextBox.Text;
                Project.OutFolder = folder;
                Project.GenerateHTML();
                SaveDefaultCSS(folder);
            }
            
        }

        private void CapturedScreen(OzFrame frame)
        {
            Project.Add(frame);
        }

        public void CaptureFrame()
        {
            if (!IsCapturing)
            {
                cFrame = new Windows.CaptureWindow();
                cFrame.Show();
                cFrame.Closing += new System.ComponentModel.CancelEventHandler(cFrame_Closing);
                cFrame.OnCaptureScreen += new Windows.CaptureWindow.CaptureScreenHandler(CapturedScreen);
                IsCapturing = true;
            }
        }

        private void InitializeEventHooks()
        {
            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
            this.GenerateButton.Click += new RoutedEventHandler(GenerateButton_Click);
            HotKey printScrnHotKey = new HotKey(Key.PrintScreen, KeyModifier.Ctrl, OnPrintScreenHandler);
        }

        private void SaveDefaultCSS(string out_path)
        {
            string[] names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
            foreach (string s in names)
            {
                if (s.EndsWith("stylesheet_default.css"))
                {
                    using (StreamReader reader = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(s)))
                    {
                        string result = reader.ReadToEnd();
                        StreamWriter writer = new StreamWriter(out_path + "stylesheet_default.css");
                        writer.Write(result);
                        writer.Close();
                    }
                }
            }
        }

        #region EVENTS

        void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            SaveProjectToFile();
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //e.Cancel = true;
            //this.WindowState = System.Windows.WindowState.Minimized;
            if (cFrame != null)
            {
                cFrame.Close();
            }
        }

        private void OnPrintScreenHandler(HotKey h)
        {
            CaptureFrame();
        }

        void cFrame_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            IsCapturing = false;
        }

        #endregion

        #endregion
    }
}
